<?php
require_once('common.inc.php');
require_once(_INC_PATH.'database.php');
require_once(_INC_PATH.'captcha.inc');
session_start();

if($_REQUEST['cmd'] == "regist"){
	$systemMsg=checkRegist();
	if(empty($systemMsg)){
		$db = new database();
		$insertRow['user_name'] = $_REQUEST['user_name'];
		$insertRow['email'] = $_REQUEST['email'];
		$insertRow['password'] = md5($_REQUEST['password']);
		$insertRow['login_time'] = date("Y-m-d H:i:s");
		$insertRow['create_time'] = date("Y-m-d H:i:s");
		$insertRow['update_time'] = date("Y-m-d H:i:s");
		$insertRow['status'] = '0';
		$user_id = $db->insert($insertRow,"tbl_user");
		if($user_id > 0)
		{
			if(_REGISTEMAILCHECK){
				$mailContent['subject']=$regist_subject;
				$mailContent['body']=$regist_body;
				$mailContent['email']=$_REQUEST['email'];
				$mailContent['name']=$_REQUEST['user_id'];
				//sendMail($mailContent);
			}
			setloginCookie($user_id);
			header("location:index.html");
		}	
	}else{
		setHiddenParam($smarty);
		$smarty->assign('systemMsg',$systemMsg);
		Display('regist.tpl');
	}
}else if($_REQUEST['cmd']=="captcha"){
	SHOWCAPTCHA();
}else if($_REQUEST['cmd']=="ajaxCheckUserName"){
	echo checkRegistUserName();
}else if($_REQUEST['cmd']=="ajaxCheckEmail"){
	echo checkRegistEmail();
}else if($_REQUEST['cmd']=="ajaxCheckPassword"){
	echo checkRegistPassword();
}else if($_REQUEST['cmd']=="ajaxCheckPasswordAgain"){
	echo checkRegistPasswordAgain();
}else if($_REQUEST['cmd']=="ajaxCheckWord"){
	echo checkRegistWord();
}else{
	Display('regist.tpl');
}
function checkRegist()
{
	$systemMsg=array();
	$ret=checkRegistUserName();
	if($ret!="1")$systemMsg['user_name']=$ret;
	$ret=checkRegistEmail();
	if($ret!="1")$systemMsg['email']=$ret;
	$ret=checkRegistPassword();
	if($ret!="1")$systemMsg['password']=$ret;
	$ret=checkRegistPasswordAgain();
	if($ret!="1")$systemMsg['password_again']=$ret;
	$ret=checkRegistWord();
	if($ret!="1")$systemMsg['word']=$ret;
	return $systemMsg;
}
function setHiddenParam(&$smarty){		
	$smarty->assign("user_name",$_REQUEST["user_name"]);
	$smarty->assign("email",$_REQUEST["email"]);
	$smarty->assign("password",$_REQUEST["password"]);
	$smarty->assign("word",$_REQUEST["word"]);
}

function checkRegistUserName(){
	$userName = trim($_REQUEST['user_name']);
	$userName = str_replace(' ','',$userName);
	if($userName == ""){
		return USERIDERROR1;
	}elseif(strpos($userName,'管理') !== false){
		return USERIDERROR4;
	}else{
		$db = new database();
		$sql="select count(*) as num from tbl_user where status='0' 
			  and user_name='".mysql_escape_string($_REQUEST['user_name'])."'";
		$db->setQuery($sql);
		$data = $db->loadRow();
		if($data['num']>=1) {
			return USERIDERROR5;
		}else{
			return '1';
		}
	}
}
function checkRegistPassword(){
	if($_REQUEST['password']==""){
		return PASSWORDERROR1;
	}elseif(strlen($_REQUEST['password'])>32||strlen($_REQUEST['password'])<6){
		return PASSWORDERROR2;
	}else{
		return '1';
	}
}
function checkRegistPasswordAgain(){
	if($_REQUEST['password_again']==""){
		return PASSWORDAGAINERROR1;
	}elseif($_REQUEST['password']!=$_REQUEST['password_again']){
		return PASSWORDAGAINERROR2;
	}else{
		return '1';
	}
}
function checkRegistEmail(){
	if($_REQUEST['email'] == ""){
		return EMAILERROR1;
	}else if(strlen($_REQUEST['email'])>100){
		return EMAILERROR2;
	}else if(!checkMail($_REQUEST['email'])){
		return EMAILERROR3;
	}else{
		$db = new database();
		$sql="select count(*) as num from tbl_user where status='0' 
			  and email='".mysql_escape_string($_REQUEST['email'])."'";
		if(_getcookie("id") != ""){
			$sql.=" and id <> '".mysql_escape_string(_getcookie("id"))."'";
		}
		$db->setQuery($sql);
		$data = $db->loadRow();
		if($data['num']>=1) {
			return EMAILERROR4;
		}else{
			return '1';
		}
	}	
}
function checkRegistWord(){
	if($_REQUEST['word']==""){
		return WORDERROR1;
	}else{
		$captcha = new captcha();
		$ret=$captcha->check_word($_REQUEST['word']);
		if(!$ret){
			return WORDERROR2;
		}else{
			return '1';
		}
	}
}
function setloginCookie($id)
{
	global $db,$cookie_field,$cookie_time;
	$sql = "select ".implode(",",$cookie_field)." from tbl_user where id='$id'";
	$data = $db->loadRow($sql);
	if($data['user_name']){
		$data['unicodeUsername'] = str2unicode($data['user_name']);
	}
	_setcookie($data, $cookie_time);
	return true;
}
?>